﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.ZGCG;
using StateGrid.GlobalVariables;

public partial class AddFL : System.Web.UI.Page
{
    private ZGCG_DAO zgcgDAO = new ZGCG_DAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        if (!this.IsPostBack)
        {
            this.BindData();
        }  
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void BindData()
    {
        //获取专业分类
        this.zyflHiddenField.Value = (String)Request["parent"];
        if (null == this.zyflHiddenField.Value || "".Equals(this.zyflHiddenField.Value))
        {
            String message = "没有获取到上级分类编码";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        this.zyflTextBox.Text = this.zyflHiddenField.Value;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        //清空Label信息
        this.ClearLabel();
        //验证分类名称是否有效
        if (!ValidateFLMC())
        {
            return;
        }
        //验证专业分类是否有效
        else if (!ValidateZYFL())
        {
            return;
        }

        //验证通过则插入新记录
        ZGCG_FL zgcgFL = new ZGCG_FL();
        zgcgFL.flmc = this.flmcTextBox.Text.Trim();
        zgcgFL.zyfl = this.zyflTextBox.Text.Trim();
        //生成分类编码
        String flbm = null;
        for (int i = 0; i < zgcgFL.zyfl.Length; i++)
        {
            flbm = flbm + zgcgFL.zyfl[i] + "0";
        }
        this.flbmTextBox.Text = flbm;
        zgcgFL.flbm = flbm;

        //分类说明
        zgcgFL.flsm = this.flsmTextBox.Text.Trim();
        zgcgDAO.InsertZGCG_FL(zgcgFL);

        Response.Write("<Script>alert('添加中国成果分类成功')</Script>");
    }

    /// <summary>
    /// 清空Label信息
    /// </summary>
    private void ClearLabel()
    {
        this.errorFLBMLabel.Text = "";
        this.errorFLMCLabel.Text = "";
    }

    /// <summary>
    /// 验证专业分类
    /// </summary>
    private Boolean ValidateZYFL()
    {
        Boolean flag = true;
        this.errorFLBMLabel.Text = "";

        if (!this.zyflTextBox.Text.StartsWith(this.zyflHiddenField.Value))
        {
            this.errorFLBMLabel.Text = "错误：专业分类不正确";
            flag = false;
        }
        else
        {
            ZGCG_FL zgcgFL = this.zgcgDAO.GetZGCG_FLByZyfl(this.zyflTextBox.Text.Trim());
            if (null != zgcgFL)
            {
                this.errorFLBMLabel.Text = "错误：该专业分类已存在";
                flag = false;
            }
        }
        return flag;
    }

    /// <summary>
    /// 验证分类名称
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateFLMC()
    {
        Boolean flag = true;
        this.errorFLMCLabel.Text = "";

        if (null == this.flmcTextBox.Text || "".Equals(this.flmcTextBox.Text.Trim()))
        {
            this.errorFLMCLabel.Text = "错误：分类名称不能为空";
            flag = false;
        }

       ZGCG_FL zgcgFL = zgcgDAO.GetZGCG_FLByFlmc(this.flmcTextBox.Text.Trim());

        if (null != zgcgFL)
        {
            this.errorFLMCLabel.Text = "错误：分类名称已存在";
            flag = false;
        }
        return flag;
    }
    
    /// <summary>
    /// 恢复分类编码初始信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.zyflTextBox.Text = this.zyflHiddenField.Value;
    }
}